package com.dure.hotmemory.request;


import com.dure.hotmemory.bean.Bean;
import com.dure.hotmemory.controllers.Controller;
import com.google.gson.Gson;
import com.socks.library.KLog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.HashMap;


/**
 * Created by dure on 16-3-31.
 */
public class RequestModel {

    private Controller controller;
    public RequestModel(Controller controller){
        this.controller = controller;
    }

    public <T extends Bean> void doGet(String url, Class<T> clazz){
        doGet(url, new HashMap<String, String>(), clazz);
    }

    public <T extends Bean> void doGet(String url, HashMap<String, String> paramMap, final Class<T> clazz){
        KLog.d("lesleixz", "url = " + url);
        RequestParams params = new RequestParams(url);

        for (String key : paramMap.keySet()){
            String value = paramMap.get(key);
            params.addQueryStringParameter(key, value);
        }
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                KLog.d("lesleixz", "result = " + result);
                Gson gson = new Gson();
                T t = gson.fromJson(result, clazz);
//                EventBus.getDefault().post(t);
                controller.handleRequestSuccess(t);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                KLog.d("lesleixz", "ex.getMessage() = " + ex.getMessage());
//                EventBus.getDefault().post(ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

    public <T extends Bean> void doPost(String url, HashMap<String, String> paramMap, final Class<T> clazz){
        KLog.d("lesleixz", "T url = " + url);
        RequestParams params = new RequestParams(url);

        for (String key : paramMap.keySet()){
            String value = paramMap.get(key);
            params.addBodyParameter(key, value);
            KLog.d("lesleixz", key+" = " + value);
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                KLog.d("lesleixz", "T result = " + result);
                Gson gson = new Gson();
                T t = gson.fromJson(result, clazz);
//                EventBus.getDefault().post(t);
                controller.handleRequestSuccess(t);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                KLog.d("lesleixz", "ex.getMessage() = " + ex.getMessage());
//                EventBus.getDefault().post(ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

    public <T extends Bean> void doPost(String url, HashMap<String, String> paramMap, File file, final Class<T> clazz){
        KLog.d("lesleixz", "T url = " + url);
        RequestParams params = new RequestParams(url);

        params.setMultipart(true);
        for (String key : paramMap.keySet()){
            String value = paramMap.get(key);
            params.addBodyParameter(key, value);
            KLog.d("lesleixz", key+" = " + value);
        }


        params.addBodyParameter("head_img", file, "image/jpeg");
//        try {
//            params.addBodyParameter(
//                    "head_img",
//                    new FileInputStream(file),
//                    "image/jpeg",
//                    // 测试中文文件名
//                    "你好.jpg");
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                KLog.d("lesleixz", "T result = " + result);
                Gson gson = new Gson();
                T t = gson.fromJson(result, clazz);
//                EventBus.getDefault().post(t);
                controller.handleRequestSuccess(t);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                KLog.d("lesleixz", "ex.getMessage() = " + ex.getMessage());
//                EventBus.getDefault().post(ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void doPost(String url, HashMap<String, String> paramMap){
        KLog.d("lesleixz", "url = " + url);
        RequestParams params = new RequestParams(url);

        for (String key : paramMap.keySet()){
            String value = paramMap.get(key);
            params.addBodyParameter(key, value);
            KLog.d("lesleixz", key+" = " + value);
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                KLog.d("lesleixz", "result = " + result);
//                controller.handleRequestSuccess(result);
//                EventBus.getDefault().post(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                KLog.d("lesleixz", "ex.getMessage() = " + ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
